package com.youxin.authserver.controller;


import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.youxin.busfoundation.bean.BaseLogModel;
import com.youxin.busfoundation.bean.ResponseObject;
import com.youxin.busfoundation.bean.log.RequestLogBean;
import com.youxin.busfoundation.bean.login.RequestLoginBean;
import com.youxin.busfoundation.bean.login.RequestLoginVerifyCode;
import com.youxin.busfoundation.bean.login.RequestRefreshToken;
import com.youxin.busfoundation.enums.SystemType;
import com.youxin.foundation.utils.ObjectUtils;
import com.youxin.foundation.utils.StringUtils;
import com.youxin.authserver.common.log.LogMethod;
import com.youxin.authserver.service.LoginService;


/**
 * 登录接口
 * 
 * @author yingde.cao
 *
 */
@RestController
public class LoginController extends BaseController{

	@Autowired
	private LoginService loginService;
	
	@RequestMapping(value = "/auth/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject login(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestLoginBean bean) {
		try {			
			ObjectUtils.requestNonNull(bean, "参数不能为空");
			ObjectUtils.requestNonNull(bean.getExtra(), "参数extra不能为空");
			return loginService.login(bean);
		} catch (Exception e) {
			logger.error("/auth/login异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	//@LogMethod(id="ent.refresh.token",eventContent={"log.staytime"})
	@RequestMapping(value = "/auth/token/refresh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject refreshToken(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestRefreshToken bean) {
		try {
			ObjectUtils.requestNonNull(bean, "参数不能为空");
			ObjectUtils.requestNonNull(bean.getToken(), "参数token不能为空");
			return loginService.refreshToken(bean);
		} catch (Exception e) {
			logger.error("/auth/token/refresh异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	@RequestMapping(value = "/auth/login/verifycode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject loginVerifyCode(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestLoginVerifyCode bean) {		
		try {
			ObjectUtils.requestNonNull(bean, "参数不能为空");
			ObjectUtils.requestNonNull(bean.getMobile(), "手机号码不能为空");
			return loginService.requestLoginVerifyCode(bean);			
		} catch (Exception e) {
			logger.error("/auth/token/refresh异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	
	@RequestMapping(value = "/auth/logout", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject loginOut(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestRefreshToken bean) {		
		try {
			ObjectUtils.requestNonNull(bean, "参数不能为空");
			ObjectUtils.requestNonNull(bean.getToken(), "参数token不能为空");

			return loginService.logout(bean,request);			
		} catch (Exception e) {
			logger.error("/auth/token/refresh异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	@Value("${server.port}")
	private String port;
	
	@RequestMapping(value = "/auth/info", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject test(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
			throws Exception {
		String host = "";
		try {
			host = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			logger.error("/auth/info异常", e);
			return ResponseObject.systemError(e);
		}
		return new ResponseObject(host + ":" + port);
	}

}
